package com.wang.wx.service.impl;

import com.wang.wx.config.BloomFilterConfig;
import com.wang.wx.constants.BatchConstants;
import com.wang.wx.constants.ThreadPoolManager;
import com.wang.wx.service.DataSplitterService;
import com.wang.wx.service.LyUserService;
import com.wang.wx.utils.CsvUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author wjh
 * @create 2024-08-02 15:59
 * 领英数据入库
 */
@Service
@Slf4j
public class LyDataSplitterServiceImpl implements DataSplitterService {

    @Value("${file.batch.size}")
    private int batchSize;
    @Resource
    private BloomFilterConfig bloomFilterConfig;
    @Value("${file.thread.pool.size}")
    private int threadPoolSize;
    @Resource
    private LyUserService lyUserService;

    @Override
    public void handleFile(String file) throws IOException {
        initPool();
        BatchConstants.LY_SIGNAL_COMPLETION.set(false);
        try {
            // 解析csv文件，CsvUtils里面已经将数据存入阻塞队列了
            CsvUtils.parsingCsvDataMethod(file);
        } finally {
            BatchConstants.LY_SIGNAL_COMPLETION.set(true);
            log.info("入库完成");
            ThreadPoolManager.shutdown();
            log.info("线程池关闭");
        }
    }

    public void initPool() {
        ThreadPoolManager.getExecutor(threadPoolSize);
        for (int i = 0; i < threadPoolSize; i++) {
            ThreadPoolManager.submitTask(() -> BatchProcessor.processLyQueue(lyUserService, batchSize));
        }
    }
}
